/*
 * Composition Knowledge Miner. A data mashup composition knowledge miner
 * Copyright (C) 2011 Carlos Rodriguez
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package eu.ict_omelette.mashup.compositionminer.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author carlos
 */
public class Carpenter {

    String[] db;
    double minSup;

    public Carpenter(String db[], double minSup){
        this.db = db;
        this.minSup = minSup * 100;
    }

    public List mine() throws IOException{
        List<int[]> result = new ArrayList<int[]>();

        try{
            //System.out.println("Time: " + System.currentTimeMillis());

            long timestamp = System.currentTimeMillis();

            String inputFileName = "input-" + timestamp + ".tmp";
            String outputFileName = "output-" + timestamp + ".tmp";

            FileWriter fstream = new FileWriter(inputFileName);
            BufferedWriter out = new BufferedWriter(fstream);
            for(int i = 0; i < db.length; i++){
                out.write(db[i] + "\n");
            }

            out.close();

            String[] cmd = {"./carpenter_macos", "-s" + minSup, "-tm", inputFileName, outputFileName};
            Process p = Runtime.getRuntime().exec(cmd);

            p.waitFor();

            File outFile = new File(outputFileName);
            if(!outFile.exists()){
                File inputFile = new File(inputFileName);
                inputFile.delete();
                
                return result;
            }

            FileInputStream fsInputStream = new FileInputStream(outputFileName);
            DataInputStream in = new DataInputStream(fsInputStream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null)   {           
                //System.out.println (strLine);

                String[] items = strLine.split("\\s+");

                int[] itemset = new int[items.length - 1];
                int k = 0;
                for(int j = 0; j < items.length; j++){
                    String item = items[j];

                    //This is the info about the support of the item (we want only the items)
                    if(item.indexOf("(") >= 0)
                        continue;

                    itemset[k] = Integer.valueOf(item);
                    k++;
                }

                result.add(itemset);

            }            
            in.close();

            File inputFile = new File(inputFileName);
            File outputFile = new File(outputFileName);

            inputFile.delete();
            outputFile.delete();

        }catch(Exception e){
            e.printStackTrace();
        }        
        
        return result;
    }

}
